package com.neuedu.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.neuedu.api.AdminService;
import com.neuedu.entity.CheckRequest;
import com.neuedu.entity.Register;
import com.neuedu.mapper.CheckRequestMapper;
import com.neuedu.service.CheckRequestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.service.MedicalTechnologyService;
import com.neuedu.service.RegisterService;
import com.neuedu.vo.RegisterCheckVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 检查申请表 服务实现类
 * </p>
 *
 * @author LiYang
 * @since 2024-11-17
 */
@Service
public class CheckRequestServiceImpl extends ServiceImpl<CheckRequestMapper, CheckRequest> implements CheckRequestService {
    @Resource
    RegisterService registerService;
    @Resource
    MedicalTechnologyService medicalTechnologyService;
    @Resource
    AdminService adminService;
    @Override
    public Boolean add(CheckRequest checkRequest, Long openId) {
        checkRequest.setCreationTime(LocalDateTime.now());
        checkRequest.setCheckEmployeeId(openId.intValue());
        checkRequest.setCheckState("已开立");
        return this.save(checkRequest);
    }

    @Override
    public RegisterCheckVo list(String caseNumber, String checkState) {
        final Register register = registerService.getByCaseNumber(caseNumber);
        LocalDate today=LocalDate.now(); //今天
        LambdaQueryWrapper<CheckRequest> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(CheckRequest::getRegisterId,register.getId())
                .eq(CheckRequest::getCheckState,checkState)
                .between(CheckRequest::getCreationTime,LocalDateTime.of(today, LocalTime.MIN),LocalDateTime.of(today,LocalTime.MAX));
        final  List<CheckRequest> list = this.list(wrapper);
        for (CheckRequest checkRequest : list) {
            checkRequest.setMedicalTechnology(medicalTechnologyService.getById(checkRequest.getMedicalTechnologyId()));
        }
        return new RegisterCheckVo(register,list);
    }

    @Override
    public Boolean updateState(Integer[] ids) {
        CheckRequest checkRequest = new CheckRequest();
        checkRequest.setCheckState("已缴费");
        LambdaUpdateWrapper<CheckRequest> wrapper=new LambdaUpdateWrapper<>();
        wrapper.in(CheckRequest::getId,ids);
        return this.update(checkRequest,wrapper);
    }
        @Override
    public List<CheckRequest> jiaofeiList(String caseNumber, String realName, String checkState) {
        QueryWrapper<CheckRequest> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(caseNumber)) {
            final Register register = registerService.getByCaseNumber(caseNumber);
            wrapper.eq("register_id", register.getId());
        }
        if (StringUtils.isNotBlank(realName)) {
            final List<Register> registerList = registerService.getByName(realName);
            List<Integer> registerIds = new ArrayList<>();
            registerList.forEach(entry -> {
                registerIds.add(entry.getId());
            });
            wrapper.in("register_id", registerIds);
        }
        LocalDate today = LocalDate.now();

        wrapper.eq("check_state", checkState)
                .between("creation_time", LocalDateTime.of(today, LocalTime.MIN), LocalDateTime.of(today, LocalTime.MAX));
        final List<CheckRequest> list = this.list(wrapper);
        list.forEach(entry -> {
            entry.setRegister(registerService.getById(entry.getRegisterId()));
            entry.setMedicalTechnology(medicalTechnologyService.getById(entry.getMedicalTechnologyId()));
        });
        return list;
    }
        @Override
    public Boolean addResult(Integer id, String text, Long openId) {
        CheckRequest checkRequest = new CheckRequest();
        checkRequest.setId(id);
        checkRequest.setCheckResult(text);
        checkRequest.setInputcheckEmployeeId(openId.intValue());
        checkRequest.setCheckTime(LocalDateTime.now());
        checkRequest.setCheckState("已出结果");
        return this.updateById(checkRequest);
    }

    @Override
    public List<CheckRequest> getResult(Integer registerId, String checkState) {
        LambdaQueryWrapper<CheckRequest> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(CheckRequest::getRegisterId,registerId)
                .eq(CheckRequest::getCheckState,checkState);
        final List<CheckRequest> list = this.list(wrapper);
        list.forEach(entry -> {
            entry.setMedicalTechnology(medicalTechnologyService.getById(entry.getMedicalTechnologyId()));
            entry.setCheckEmpName(adminService.getNameById(entry.getInputcheckEmployeeId().longValue()));
        });
        return list;
    }
}
